package com.bll.tsdoa.utils;

/**
 * @author bll
 * @version 1.0
 * @date 2025/3/3 00:38
 */

import com.bll.tsdoa.entity.Part;
import com.bll.tsdoa.entity.Raw;
import com.bll.tsdoa.mapper.PartDao;
import com.bll.tsdoa.mapper.RawDao;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@Component
public class OpenCVUtils {
    // 静态代码块加载OpenCV库（需根据实际环境配置）
//    static {
//        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//    }

    @Resource
    private RawDao rawDao;
    @Resource
    private PartDao partDao;

    /**
     * 根据染色体绘制排样方案图
     *
     * @param decoded 染色体（二进制编码数组）
     */
    public void draw(int[] decoded) {
        // 1. 解码染色体
//        int[] decoded = decode(chrom, this.bitLength);
        int rawId = decoded[0];
        int[] partNums = Arrays.copyOfRange(decoded, 1, decoded.length - 1);

        // 2. 查询数据库获取原料和零件信息
        Raw raw = rawDao.selectById(rawId);
        List<Part> parts = partDao.selectList(null);
        if (raw == null || parts == null || parts.size() != partNums.length) {
            throw new IllegalArgumentException("数据查询失败或零件数量不匹配");
        }

        // 3. 初始化画布参数
        int imgHeight = 200; // 图像高度
        int baseY = 100;     // 原料线段基准Y坐标
        double rawLength = raw.getLength();
        int imgWidth = (int) (rawLength * 10) + 200; // 按1:10比例缩放（可根据实际调整）
        Mat image = new Mat(imgHeight, imgWidth, CvType.CV_8UC3, new Scalar(255, 255, 255));

        // 4. 绘制原料线段
        Scalar rawColor = new Scalar(0, 0, 0); // 黑色
        Imgproc.line(image, new Point(50, baseY), new Point(50 + rawLength * 10, baseY), rawColor, 3);
        // 标注原料信息
        String rawLabel = String.format("Raw %d (%.2f)", rawId, rawLength);
        Imgproc.putText(image, rawLabel, new Point(50, baseY - 20),
                Imgproc.FONT_HERSHEY_SIMPLEX, 0.6, rawColor, 2);

        // 5. 绘制零件切割段
        double currentPos = 50; // 起始X坐标
        Random rand = new Random();
        for (int i = 0; i < partNums.length; i++) {
            if (partNums[i] == 0) continue;

            Part part = parts.get(i);
            double partLength = part.getLength();
            // 生成随机颜色区分零件
            Scalar partColor = new Scalar(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

            // 绘制每个零件的切割段
            for (int j = 0; j < partNums[i]; j++) {
                double endPos = currentPos + partLength * 10;
                // 绘制线段
                Imgproc.line(image,
                        new Point(currentPos, baseY),
                        new Point(endPos, baseY),
                        partColor, 8); // 粗线表示零件
                // 标注零件信息
                String label = String.format("%s (x%d)", part.getId(), partNums[i]);
                Imgproc.putText(image, label,
                        new Point(currentPos + 5, baseY - 30),
                        Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, partColor, 1);
                currentPos = endPos;
            }
        }

        // 6. 保存图像
        String imgPath = "img/layout_result.png";
        new File("img").mkdirs(); // 创建目录（如果不存在）
        Imgcodecs.imwrite(imgPath, image);
        image.release(); // 释放内存
    }
}